package com.zjitc.service.imp;

import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zjitc.dao.ProductDao;
import com.zjitc.model.Product;
import com.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import redis.clients.jedis.Jedis;
import sun.plugin2.gluegen.runtime.CPU;

/**
 * Created by LWW on 2018/4/19.
 */
@Service
public class ProductServiceImp implements ProductService {

  @Autowired
  private ProductDao productDao;

  @Autowired
  Jedis jedis;

  @Override
  public List<Product> findNewProduct() {
    Gson gson= new Gson();
    String newProduct=jedis.get("newProduct");
    List<Product> list = gson.fromJson(newProduct, new TypeToken<List<Product>>() {
    }.getType());
    return list;
  }

  @Override
  public List<Product> findHotProduct() {
    Gson gson= new Gson();
    String newProduct=jedis.get("hotProduct");
    List<Product> list = gson.fromJson(newProduct, new TypeToken<List<Product>>() {
    }.getType());
    return list;
  }

  @Override
  public List<Product> findByCid(String cid, Integer pageNumber, Integer count) {
    PageHelper.startPage(pageNumber, count);
    List<Product> products = productDao.findByCid(cid);
    return products;
  }

  @Override
  public Product findProductInfo(String pid) {
    return productDao.findProductInfo(pid);
  }

  @Override
  public List<Product> findAllProduct(Integer pageNum, Integer count) {
    PageHelper.startPage(pageNum, count);
    List<Product> allProduct = productDao.findAllProduct();
    return  allProduct;
  }

  @Override
  public Product findByPid(String pid) {
    return productDao.findByPid(pid);
  }

  @Override
  public void editProduct(Product product) {
    productDao.editProduct(product);
  }

  @Override
  public void deleteProduct(String pid) {
    productDao.deleteProduct(pid);
  }

  @Override
  public void addProduct(Product product) {
    productDao.addProduct(product);
  }


}
